<?php

function reequipment_admin_areas()
{
	// reminder, as you add phases, they need to be added to the allowed input.
    if (!xarVarFetch('phase', 'enum:form:create:delete:edit:update', $phase, 'form', XARVAR_NOT_REQUIRED, XARVAR_PREP_FOR_DISPLAY)) return;
    if (!xarVarFetch('area_name', 'str:1:', $area_name, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('area_id', 'int:1:', $area_id, '', XARVAR_NOT_REQUIRED)) return;
    
    if (!xarSecurityCheck('AdminEquipment', 1)) return;
    $data = array();
    
    switch(strtolower($phase)) {
        case 'form':
            $data['areas'] = xarModAPIFunc('reequipment', 'user', 'getareas');
            $data['form_label'] = xarML('Add an Area');
            $data['form_area_name'] = '';
            $data['form_phase'] = 'create';
            $data['form_area_id'] = '';
        break;

        case 'create':
        		if (empty($area_name)) {
        			xarResponseRedirect(xarModURL('reequipment', 'admin', 'areas'));
        		}
        		$postinfo = xarModAPIFunc('reequipment', 'admin', 'createareas', array('area_name' => $area_name));
                xarResponseRedirect(xarModURL('reequipment', 'admin', 'areas'));
        break;
         
        case 'delete':
                $postinfo = xarModAPIFunc('reequipment', 'admin', 'deleteareas', array('area_id' => $area_id));
                xarResponseRedirect(xarModURL('reequipment', 'admin', 'areas'));
        break;  
        
        case 'edit':
            $data['areas'] = xarModAPIFunc('reequipment', 'user', 'getareas');
            $data['form_label'] = xarML('Edit an Area');
            $form_var = xarModAPIFunc('reequipment', 'user', 'getareas', array('area_id' => $area_id));
            foreach($form_var as $var){
            	$data['form_area_name'] = $var['area_name'];
            	$data['form_area_id'] = $var['area_id'];
            }
            $data['form_phase'] = 'update';
        break;
        
		case 'update':
			if (empty($area_name)) {
				xarResponseRedirect(xarModURL('reequipment', 'admin', 'areas'));
			}
			$postinfo = xarModAPIFunc('reequipment', 'admin', 'updateareas', array('area_name' => $area_name, 'area_id' => $area_id));
			xarResponseRedirect(xarModURL('reequipment', 'admin', 'areas'));
        break;
        
    }

    // Return the output
    return $data;
}

?>